package com.msoe.client.panels;

import java.util.HashMap;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.msoe.client.Session;
import com.msoe.client.panels.eventhandlers.LoginEventHandler;

public class Login implements MSOERPanel {

	public static final String DIV_ID = "login";

	private VerticalPanel loginPanel;
	private DecoratorPanel decoration;
	private Session session;

	private TextBox T_user = new TextBox();
	private TextBox T_pass = new PasswordTextBox();

	public Login(Session session){
		this.session = session;

		loginPanel = new VerticalPanel();
		decoration = new DecoratorPanel();
		decoration.add(loginPanel);

		HorizontalPanel TitlePanel = new HorizontalPanel();
		HorizontalPanel UserNamePanel = new HorizontalPanel();
		HorizontalPanel PasswordPanel = new HorizontalPanel();
		HorizontalPanel ButtonPanel = new HorizontalPanel();
		ButtonPanel.addStyleDependentName("LoginButtonsCenter");

		//Components
		HTML B_register = new HTML();
		B_register.setWordWrap(true);
		B_register.setHTML("Don't have an account? <a href='javascript:;'>Register!</a>");
		Label L_title = new Label("MSOE Ride Login");
		L_title.addStyleDependentName("LargeText");
		Label L_user = new Label("Username:");
		Label L_pass = new Label("Password:");
		Button B_submit = new Button("Submit");
		
		T_user.setWidth("150");
		T_pass.setWidth("150");

		//Assemble horizontal panels
		TitlePanel.add(L_title);

		UserNamePanel.add(L_user);
		UserNamePanel.add(T_user);

		PasswordPanel.add(L_pass);
		PasswordPanel.add(T_pass);
		PasswordPanel.add(B_submit);

		ButtonPanel.add(B_register);

		//Add elements to the parent Panel
		loginPanel.add(TitlePanel);
		loginPanel.add(UserNamePanel);
		loginPanel.add(PasswordPanel);
		loginPanel.add(ButtonPanel);


		//Create listeners for the buttons
		LoginEventHandler handler = new LoginEventHandler(this);
		B_register.addClickListener( handler );
		B_submit.addClickListener( handler );
		T_pass.addKeyboardListener(handler);
	}

	public void show(){
		if(!session.validate()){
			RootPanel.get().clear();
			RootPanel.get(DIV_ID).add(decoration);
		}
		else{
			PanelMap.getPanel("UserHome").show();
		}
	}

	public void hide(){
		decoration.removeFromParent();
	}
	
	public HashMap<String, String> getFields(){
		HashMap<String, String> fields = new HashMap<String, String>();
		
		fields.put("username", T_user.getText());
		fields.put("password", T_pass.getText());
		
		return fields;
	}

	public Session getSession() {
		return session;
	}

}
